package wwcontrolserver.datarecorders;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.File;
import org.tmatesoft.sqljet.core.SqlJetException;
import org.tmatesoft.sqljet.core.SqlJetTransactionMode;
import org.tmatesoft.sqljet.core.table.ISqlJetTable;
//import org.tmatesoft.sqljet.core.table.ISqlJetTransaction;
import org.tmatesoft.sqljet.core.table.SqlJetDb;
import java.util.Date;
/**
 *
 * @author Michael Ferry
 */
public class WWDatabaseConnection extends AbstractDataRecorder {

    //private String fSep = System.getProperty("file.separator");
    //protected static final String DB_NAME = "PhidgetsData_06202012.sqlite";
    protected static final String SENSOR_TABLE_NAME = "sensor";
    protected static final String DO_TABLE_NAME = "DO";
    protected static final String DI_TABLE_NAME = "DI";
    protected static final String ATTACH_LOG_TABLE_NAME = "Attach_Log";
    protected final SqlJetDb OutputDatabase;
    protected final ISqlJetTable SensorTable;
    protected final ISqlJetTable DOTable;
    protected final ISqlJetTable DITable;
    protected final ISqlJetTable AttachTable;
    private final WWControlServerLogger WWCSLog;

    public WWDatabaseConnection(WWControlServerLogger WWCSLog,File dbFile) throws SqlJetException{
        this.WWCSLog = WWCSLog;
        try {
//
            OutputDatabase = SqlJetDb.open(dbFile, true);
            OutputDatabase.beginTransaction(SqlJetTransactionMode.WRITE);

//            System.out.println();
//            System.out.println("&gt;Database schema objects:");
//            System.out.println();
//            System.out.println(OutputDatabase.getSchema());
//            System.out.println(OutputDatabase.getOptions());
            
            WWCSLog.RecordLogEntry("Database schema objects:");
            WWCSLog.RecordLogEntry(OutputDatabase.getSchema().toString());
            WWCSLog.RecordLogEntry(OutputDatabase.getOptions().toString());

            SensorTable = OutputDatabase.getTable(SENSOR_TABLE_NAME);
            DOTable = OutputDatabase.getTable(DO_TABLE_NAME);
            DITable = OutputDatabase.getTable(DI_TABLE_NAME);
            AttachTable = OutputDatabase.getTable(ATTACH_LOG_TABLE_NAME);
            //SensorTable.insert(System.currentTimeMillis(), 124234, 105, 2343, 53.6, 1, 10);
//            OutputDatabase.commit();

        }finally {
            CommitDatabase();
        }
    }

    @Override
    public void InsertSensorRecord(int SerialNumber, int SensorIndex, int sval,
            double cval, int ratiometric, int sensitivity, double DYNR, int Attached) {
        try {

            SensorTable.insert(System.currentTimeMillis(), SerialNumber, SensorIndex, sval,
                    cval, ratiometric, sensitivity, DYNR, Attached);
        } catch (SqlJetException sqle) {
            WWCSLog.RecordExceptionLogEntry(sqle);

        } finally {
            CommitDatabase();
        }
    }
    @Override
    public void InsertSensorRecord(Date currtime, int SerialNumber, int SensorIndex, int sval,
            double cval, int ratiometric, int sensitivity, double DYNR, int Attached) {
        try {

            SensorTable.insert(currtime.getTime(), SerialNumber, SensorIndex, sval,
                    cval, ratiometric, sensitivity, DYNR, Attached);

        } catch (SqlJetException sqle) {
            WWCSLog.RecordExceptionLogEntry(sqle);

        } finally {
            CommitDatabase();
        }
    }
    @Override
    public void InsertSensorRecordNA(int SerialNumber) {
        try {

            SensorTable.insert(System.currentTimeMillis(), SerialNumber, null, null,
                    null, null, null, null, 0);

        } catch (SqlJetException sqle) {
            WWCSLog.RecordExceptionLogEntry(sqle);

        } finally {
            CommitDatabase();
        }
    }
    @Override
    public void InsertDORecord(int SerialNumber, int DOIndex, boolean state) {
        int StateInt = state? 1 : 0;
        try {
            
            DOTable.insert(System.currentTimeMillis(), SerialNumber, DOIndex, StateInt);
                   
        } catch (SqlJetException sqle) {
            WWCSLog.RecordExceptionLogEntry(sqle);
            
        } finally {
            CommitDatabase();
        }
    }
    @Override
    public void InsertDIRecord(int SerialNumber, int DIIndex, boolean state) {
        int StateInt = state? 1 : 0;
        try {
            
            DITable.insert(System.currentTimeMillis(), SerialNumber, DIIndex, StateInt);
                   
        } catch (SqlJetException sqle) {
            WWCSLog.RecordExceptionLogEntry(sqle);
            
        } finally {
            CommitDatabase();
        }
    }
    @Override
    public void InsertAttachDetachRecord(int SerialNumber, boolean EventType) {
        int StateInt = EventType? 1 : 0;
        try{
            AttachTable.insert(System.currentTimeMillis(), SerialNumber, StateInt);
        
        } catch (SqlJetException sqle) {
            WWCSLog.RecordExceptionLogEntry(sqle);      

        } finally {
            CommitDatabase();
        }
    }
    
    @Override
    public void close() {
        try {
            OutputDatabase.close();
        } catch (SqlJetException sqle) {
            WWCSLog.RecordExceptionLogEntry(sqle);
        }
    }
    public final void CommitDatabase(){
        try {
                OutputDatabase.commit();
            } catch (SqlJetException sqle) {
                WWCSLog.RecordExceptionLogEntry(sqle);
            }
    }
}